package com.ay277.htqoa.mail;

import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import javax.mail.*;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;
import java.util.Properties;

/**
 * Created by accp09 on 2016/12/23.
 */
@Component
public class TestMail {
    @Scheduled(cron = "10,30,50 * * * * ?")
    public void mail()throws Exception {
        Properties props = System.getProperties();
        //设置邮件服务地址
        props.put("mail.smtp.host","smtp.163.com");
        //设置邮件认证方式
        props.put("mail.smtp.auth", "true");
        //邮件传输协议
        props.put("mail.transport.protocol","smtp");
        //设置加密连接
        props.put("mail.smtp.ssl.enable", "true");
        props.setProperty("mail.smtp.ssl.socketFactory","javax.net.ssl.SSLSocketFactory");

        Session session = Session.getDefaultInstance(props, new Authenticator() {
            @Override
            protected PasswordAuthentication getPasswordAuthentication() {
                return new PasswordAuthentication("15296702693@163.com","7758521a");
            }
        });
        MimeMessage message = new MimeMessage(session);
        InternetAddress form=new InternetAddress("15296702693@163.com");
        message.setFrom(form);
        InternetAddress to = new InternetAddress("269300239@qq.com");
        message.setRecipient(Message.RecipientType.TO,to);
        message.setSubject("练习邮件标题");
        message.setText("发送邮件联系作业 薛祥东");
        Transport.send(message);
        System.out.println("ok..................");
    }
}
